package com.baomibing.flow.enginee;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomibing.flow.constant.ProcessTypeEnum;
import com.baomibing.flow.execute.Execution;
import com.baomibing.flow.model.ForkNode;
import com.baomibing.flow.model.WorkFlow;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

/**
 * ForkProcess
 *
 * @author frog 2023/9/20 16:58
 * @version 1.0.0
 **/
@Getter @ToString
public class ForkProcess extends Process{

    @Getter
    private final ForkNode node;

    @JSONField(deserialize=false)
    @Setter
    private List<Process> processes;

    public ForkProcess(WorkFlow flow, ForkNode node) {
        super(flow);
        this.processType = ProcessTypeEnum.FORK.name();
        this.node = node;
    }

    @Override
    public boolean beComplete() {
        for (Process p : processes) {
            if (!p.beComplete()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean doProcess(Execution execution) {
        for (Process p : processes) {
            if (p.doProcess(execution)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean doReject(Execution execution) {
        for (Process p : processes) {
            if (p.doReject(execution)) {
                return true;
            }
        }
        return false;
    }
}
